﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui
{
    public partial class UiAllAccounts : Form
    {
        DataTable t = new DataTable();
        BindingSource b = new BindingSource();
        Controller c;
        public UiAllAccounts(Controller givenC, DataTable table)
        {
            InitializeComponent();

            c = givenC;
            t = table;
            fillData(t);
            viewUsers.Columns[0].FillWeight = 15;
            this.Show();
        }

        public void fillData(DataTable t)
        {
            // Render data onto the screen
            b.DataSource = t;
            viewUsers.DataSource = b;
        }

        private void viewUsers_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 row = (int)viewUsers.SelectedCells[0].RowIndex; //get what row the user is clicking
            Int32 col = 0; // we always want the value at the first column since this is the ID
            String val = viewUsers.Rows[row].Cells[col].FormattedValue.ToString();
            c.OpenUiEditAccount(val);
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            t = c.GetUserAsDataTable();
            viewUsers.Invalidate();
            fillData(t);
            viewUsers.Invalidate();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            Popup.FunctionNotImplemented();
        }
    }
}
